package tech.wetech.admin.modules.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.wetech.admin.modules.base.service.impl.BaseService;
import tech.wetech.admin.modules.system.mapper.AliPayInfoMapper;
import tech.wetech.admin.modules.system.mapper.ChannelInfoMapper;
import tech.wetech.admin.modules.system.mapper.PosMapper;
import tech.wetech.admin.modules.system.mapper.WXPayInfoMapper;
import tech.wetech.admin.modules.system.po.*;
import tech.wetech.admin.modules.system.service.PosService;
import tech.wetech.admin.modules.system.vo.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lu
 * @description 设备信息实体类
 * @date 2019年07月05日 17:43
 */
@Service
@Transactional
public class PosServiceImpl extends BaseService<Pos> implements PosService {

    @Autowired
    private PosMapper posMapper;

    @Autowired
    private ChannelInfoMapper channelInfoMapper;

    @Autowired
    private WXPayInfoMapper wxPayInfoMapper;

    @Autowired
    private AliPayInfoMapper aliPayInfoMapper;
    /*
    ** @author lu
    * @date 2019/7/8 14:52
    * @param [id]
    * @return java.util.List<tech.wetech.admin.modules.system.po.ChannelInfo>
    * @description 查询设备的全部数据
    */
    @Override
    public List<ChannelInfo> selectpayCompanyInfo(Integer id) {
        //把查询出来的设备明细表的支付信息json转成实体类
        ObjectMapper objectMapper = new ObjectMapper();

        ChannelInfo channelInfo=new ChannelInfo();
        List<ChannelInfo> list=new ArrayList<>();
        channelInfo.setPosId(id);
        list= channelInfoMapper.select(channelInfo);
        String json="";
        String cshJson="";
        String buSinfo="";
        for (ChannelInfo info : list) {
            json =info.getPayJson();
            if (info.getPayChannel()==1){
                info.setWxPayJsonVo(JSONObject.parseObject(json, WXPayJsonVo.class));
            }else if (info.getPayChannel() ==2){
                info.setZfbPayJsonVo(JSONObject.parseObject(json, ZFBPayJsonVo.class));
            }else if (info.getPayChannel() ==3){
                info.setIcbcVo(JSONObject.parseObject(json, ICBCVo.class));
            }else if (info.getPayChannel() ==4){
                info.setUnionJsonPayVo(JSONObject.parseObject(json,UnionJsonPayVo.class));
            }else if (info.getPayChannel() ==5){
                info.setQingDaoNongShangJsonVo(JSONObject.parseObject(json,QingDaoNongShangJsonVo.class));
            }else if (info.getPayChannel() ==6){
                info.setShouQianBaVo(JSONObject.parseObject(json,ShouQianBaVo.class));
            }else if (info.getPayChannel() ==7){
                info.setCcbbVo(JSONObject.parseObject(json,CCBBVo.class));
            }else if (info.getPayChannel() ==8){
                info.setKancloudVo(JSONObject.parseObject(json,KancloudVo.class));
            }else if (info.getPayChannel() ==9){
                info.setLeShua(JSONObject.parseObject(json,LeShua.class));
            }else if (info.getPayChannel() ==10){
                info.setChinaums(JSONObject.parseObject(json,Chinaums.class));
            }else if (info.getPayChannel() ==11){
                info.setUnionSchool(JSONObject.parseObject(json,UnionSchool.class));
            }
        }
        return list;
    }

    @Override
    public void createPos(Pos pos) {
        //获取服务器的时间
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        try {
            pos.setCreateTime(dd.parse(dd.format(new Date())));
            pos.setChangeTime(dd.parse(dd.format(new Date())));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        posMapper.insertSelective(pos);

        byte payChannel;
        //循环数组，选中那个支付渠道新增入库
        String json="";
        String cshJson="";
        String buSinfo="";
        for (ChannelInfo channelInfo : pos.getChannelInfos()) {
            if (channelInfo.getWxPayJsonVo()!=null){
                //微信
                //这是选中微信的渠道，1是微信
                payChannel=1;
                channelInfo.setCompanyName(pos.getCompanyName());
                channelInfo.setPayChannel(payChannel);
                //转成json格式
                json = JSON.toJSONString(channelInfo.getWxPayJsonVo());
                WXPayInfo wxPayInfo=new WXPayInfo();
                wxPayInfo.setAppid(channelInfo.getWxPayJsonVo().getAppID());
                wxPayInfo.setMchId(channelInfo.getWxPayJsonVo().getMchid());
                wxPayInfo.setWXkey(channelInfo.getWxPayJsonVo().getKey());
                wxPayInfo.setCreateTime(d);
                wxPayInfoMapper.insert(wxPayInfo);
            }else if (channelInfo.getIcbcVo()!=null ){
                payChannel=3;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());
                json = JSON.toJSONString(channelInfo.getIcbcVo());
            }else if (channelInfo.getZfbPayJsonVo()!=null){
                //支付宝，
                //这是选中支付宝的渠道，2是支付宝
                payChannel=2;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                //转成json格式
                json = JSON.toJSONString(channelInfo.getZfbPayJsonVo());
                AliPayInfo aliPayInfo=new AliPayInfo();
                aliPayInfo.setAliAppid(channelInfo.getZfbPayJsonVo().getAppID());
                aliPayInfo.setAliPayPublicKey(channelInfo.getZfbPayJsonVo().getAlipayPublicKey());
                aliPayInfo.setCreateTime(new Date());
                aliPayInfoMapper.insert(aliPayInfo);
            }else if (channelInfo.getUnionJsonPayVo()!=null){
                //银联支付，
                //这是选中银联支付的渠道，4是银联支付
                payChannel=4;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                //转成json格式
                json = JSON.toJSONString(channelInfo.getUnionJsonPayVo());
            }else if (channelInfo.getQingDaoNongShangJsonVo()!=null){
                //青岛农村，
                //这是选中青岛农村的渠道，5是青岛农村
                payChannel=5;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                //转成json格式
                json = JSON.toJSONString(channelInfo.getQingDaoNongShangJsonVo());
            }else if (channelInfo.getShouQianBaVo()!=null){
                //青岛农村，
                //这是选中青岛农村的渠道，5是青岛农村
                payChannel=6;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                //转成json格式
                json = JSON.toJSONString(channelInfo.getShouQianBaVo());
            }else if (channelInfo.getCcbbVo()!=null){
                //龙支付，
                //这是选中龙支付的渠道，5是青岛农村
                payChannel=7;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                //转成json格式
                json = JSON.toJSONString(channelInfo.getCcbbVo());
            }else if (channelInfo.getKancloudVo()!=null){
                //移领，
                //这是选中移领的渠道，8是移领
                payChannel=8;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                //转成json格式
                json = JSON.toJSONString(channelInfo.getKancloudVo());
            }else if (channelInfo.getLeShua()!=null){
                //乐刷，
                //这是选中乐刷的渠道，8是移领
                payChannel=9;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                //转成json格式
                json = JSON.toJSONString(channelInfo.getLeShua());
            }else if (channelInfo.getChinaums()!=null){
                //大华捷通，

                payChannel=10;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                //转成json格式
                json = JSON.toJSONString(channelInfo.getChinaums());
            }else if (channelInfo.getUnionSchool()!=null){
                //银联校园版，

                payChannel=11;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                //转成json格式
                json = JSON.toJSONString(channelInfo.getUnionSchool());
            }else if (channelInfo.getKsrcbVo()!=null){
                //江苏昆山农商银行，

                payChannel=13;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                //转成json格式
                json = JSON.toJSONString(channelInfo.getKsrcbVo());
            }
            channelInfo.setCompanyId(pos.getCompanyId());
            channelInfo.setPosId(pos.getPosNo());
            channelInfo.setPayJson(json);
            channelInfoMapper.insertSelective(channelInfo);
        }
    }

    /*
    ** @author lu
    * @date 2019/7/8 17:35
    * @param [pos]
    * @return void
    * @description 修改
    */
    @Override
    public void updatePos(Pos pos) {
        //获取服务器的时间
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        try {
            pos.setChangeTime(dd.parse(dd.format(new Date())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        posMapper.updateByPrimaryKeySelective(pos);

        //获取设备表下的明细表的ID，删除，再新增
        String strId=pos.getOldPayChannel();
        String[] strArr = strId.split(",");
        ChannelInfo channelInfo1 =new ChannelInfo();
        for (int i = 0; i < strArr.length; ++i){
            if (!strArr[i].equals("")){
                ChannelInfo channelInfo=channelInfoMapper.selectByPrimaryKey(strArr[i]);
                if (channelInfo.getPayChannel()==1){
                    //微信
                    WXPayJsonVo wxPayJsonVo= JSONObject.parseObject(channelInfo.getPayJson(),WXPayJsonVo.class);
                    WXPayInfo wxPayInfo=new WXPayInfo();
                    wxPayInfo.setAppid(wxPayJsonVo.getAppID());
                    wxPayInfo.setMchId(wxPayJsonVo.getMchid());
                    wxPayInfoMapper.delete(wxPayInfo);
                }else if(channelInfo.getPayChannel()==2){
                    //支付宝
                    ZFBPayJsonVo zfbPayJsonVo=JSONObject.parseObject(channelInfo.getPayJson(),ZFBPayJsonVo.class);
                    AliPayInfo aliPayInfo=new AliPayInfo();
                    aliPayInfo.setAliAppid(zfbPayJsonVo.getAppID());
                    aliPayInfo.setAliPayPublicKey(zfbPayJsonVo.getAlipayPublicKey());
                    aliPayInfoMapper.delete(aliPayInfo);
                }else if(channelInfo.getPayChannel()==3){
                    //支付宝公交
                    ZFBPayJsonVo zfbPayJsonVo=JSONObject.parseObject(channelInfo.getPayJson(),ZFBPayJsonVo.class);
                    AliPayInfo aliPayInfo=new AliPayInfo();
                    aliPayInfo.setAliAppid(zfbPayJsonVo.getAppID());
                    aliPayInfo.setAliPayPublicKey(zfbPayJsonVo.getAlipayPublicKey());
                    aliPayInfoMapper.delete(aliPayInfo);
                }
                channelInfo1.setId(Integer.valueOf(strArr[i]));
                channelInfoMapper.delete(channelInfo1);
            }

        }

        byte payChannel;
        String json="";
        for (ChannelInfo channelInfo : pos.getChannelInfos()) {
            if (channelInfo.getWxPayJsonVo()!=null){
                payChannel=1;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                json = JSON.toJSONString(channelInfo.getWxPayJsonVo());
                WXPayInfo wxPayInfo=new WXPayInfo();
                wxPayInfo.setAppid(channelInfo.getWxPayJsonVo().getAppID());
                wxPayInfo.setMchId(channelInfo.getWxPayJsonVo().getMchid());
                wxPayInfo.setWXkey(channelInfo.getWxPayJsonVo().getKey());
                wxPayInfo.setCreateTime(d);
                wxPayInfoMapper.insert(wxPayInfo);
            }else if (channelInfo.getIcbcVo()!=null){
                //工行，
                payChannel=3;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                json = JSON.toJSONString(channelInfo.getIcbcVo());
            }else if (channelInfo.getZfbPayJsonVo()!=null){
                payChannel=2;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());

                json = JSON.toJSONString(channelInfo.getZfbPayJsonVo());
                AliPayInfo aliPayInfo=new AliPayInfo();
                aliPayInfo.setAliAppid(channelInfo.getZfbPayJsonVo().getAppID());
                aliPayInfo.setAliPayPublicKey(channelInfo.getZfbPayJsonVo().getAlipayPublicKey());
                aliPayInfo.setCreateTime(new Date());
                aliPayInfoMapper.insert(aliPayInfo);
            }else if (channelInfo.getUnionJsonPayVo()!=null){
                payChannel=4;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());
                json = JSON.toJSONString(channelInfo.getUnionJsonPayVo());
            }else if (channelInfo.getQingDaoNongShangJsonVo()!=null){
                payChannel=5;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());
                json = JSON.toJSONString(channelInfo.getQingDaoNongShangJsonVo());
            }else if (channelInfo.getShouQianBaVo()!=null){
                payChannel=6;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());
                json = JSON.toJSONString(channelInfo.getShouQianBaVo());
            }else if (channelInfo.getCcbbVo()!=null){
                payChannel=7;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());
                json = JSON.toJSONString(channelInfo.getCcbbVo());
            }else if (channelInfo.getKancloudVo()!=null){
                payChannel=8;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());
                json = JSON.toJSONString(channelInfo.getKancloudVo());
            }else if (channelInfo.getLeShua()!=null){
                payChannel=9;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());
                json = JSON.toJSONString(channelInfo.getLeShua());
            }else if (channelInfo.getChinaums()!=null){
                payChannel=10;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());
                json = JSON.toJSONString(channelInfo.getChinaums());
            }else if (channelInfo.getUnionSchool()!=null){
                payChannel=11;
                channelInfo.setPayChannel(payChannel);
                channelInfo.setCompanyName(pos.getCompanyName());
                json = JSON.toJSONString(channelInfo.getUnionSchool());
            }
            channelInfo.setCompanyId(pos.getCompanyId());
            channelInfo.setPosId(pos.getPosNo());
            channelInfo.setPayJson(json);
            channelInfoMapper.insertSelective(channelInfo);
        }
    }

    /*
    ** @author lu
    * @date 2019/7/9 9:33
    * @param [ids]
    * @return void
    * @description设备删除
    */
    @Override
    public void deleteBatchByIds(Object[] ids) {
        ChannelInfo channelInfo =new ChannelInfo();
        for (int i = 0; i < ids.length; i++) {
            String strId=ids[i].toString();
            Pos pos=new Pos();
            pos.setId(Integer.valueOf(strId));
            pos=posMapper.selectOne(pos);
            channelInfo.setPosId(pos.getPosNo());
            //查询出配置文件,再删除
            List<ChannelInfo> list=channelInfoMapper.select(channelInfo);
            for (ChannelInfo info : list) {
                if (info.getPayChannel()==1){
                    //微信
                    WXPayJsonVo wxPayJsonVo= JSONObject.parseObject(info.getPayJson(),WXPayJsonVo.class);
                    WXPayInfo wxPayInfo=new WXPayInfo();
                    wxPayInfo.setAppid(wxPayJsonVo.getAppID());
                    wxPayInfo.setMchId(wxPayJsonVo.getMchid());
                    wxPayInfoMapper.delete(wxPayInfo);
                }else if(info.getPayChannel()==2){
                    //支付宝
                    ZFBPayJsonVo zfbPayJsonVo=JSONObject.parseObject(info.getPayJson(),ZFBPayJsonVo.class);
                    AliPayInfo aliPayInfo=new AliPayInfo();
                    aliPayInfo.setAliAppid(zfbPayJsonVo.getAppID());
                    aliPayInfo.setAliPayPublicKey(zfbPayJsonVo.getAlipayPublicKey());
                    aliPayInfoMapper.delete(aliPayInfo);
                }else if(info.getPayChannel()==3){
                    //支付宝公交
                    ZFBPayJsonVo zfbPayJsonVo=JSONObject.parseObject(info.getPayJson(),ZFBPayJsonVo.class);
                    AliPayInfo aliPayInfo=new AliPayInfo();
                    aliPayInfo.setAliAppid(zfbPayJsonVo.getAppID());
                    aliPayInfo.setAliPayPublicKey(zfbPayJsonVo.getAlipayPublicKey());
                    aliPayInfoMapper.delete(aliPayInfo);
                }
            }
            channelInfoMapper.delete(channelInfo);
            posMapper.deleteByPrimaryKey(Integer.valueOf(strId));
        }
    }

    /*
    ** @author lu
    * @date 2019/8/30 10:19
    * @param [daoRu]
    * @return void
    * @description 将获取到的txt内容存入数据库
    */
    @Override
    public void daoRU(DaoRu daoRu) {
        //解析读取的文本的数据
        //String[] arr= JSON.parseArray(daoRu.getFilename()).toArray(new String[0]);
        JSONArray contractList = JSON.parseArray(daoRu.getFilename());
        for (Object o : contractList) {
            Map<String,String> map= (Map<String, String>) JSON.parse(o.toString());
            Map<String,String> maps= new HashMap<>();
            Integer intPos=Integer.valueOf(map.get("pos_id"));
            if (daoRu.getPayChannel().equals("5")){
                maps.put("devsn",map.get("devsn"));
                maps.put("commid",map.get("commid"));
                maps.put("commstoreno",map.get("commstoreno"));
                maps.put("commoperid",map.get("commoperid"));
                maps.put("key",map.get("key"));
            }else if (daoRu.getPayChannel().equals("7")){
                maps.put("bRANCHID",map.get("bRANCHID").trim());
                maps.put("mERCHANTID",map.get("mERCHANTID").trim());
                maps.put("pOSID",map.get("pOSID"));
                maps.put("publicKey",map.get("publicKey").trim());
            }
            ChannelInfo channelInfo=new ChannelInfo();
            channelInfo.setPosId(intPos);
            String json=JSON.toJSONString(maps);
            channelInfo.setCompanyName(daoRu.getCompanyName());
            channelInfo.setCompanyId(daoRu.getCompanyId());
            channelInfo.setPayType(daoRu.getPayType());
            channelInfo.setPayChannel(Byte.valueOf(daoRu.getPayChannel()));
            channelInfo.setPayJson(json);
            channelInfoMapper.insertSelective(channelInfo);
            Pos pos=new Pos();
            pos.setCompanyId(daoRu.getCompanyId());
            pos.setCompanyName(daoRu.getCompanyName());
            pos.setPosNo(intPos);
            pos.setPosName(intPos.toString());
            switch (daoRu.getPayChannel()){
                case "1":
                    pos.setPayChannel("10000000000000000000");
                    break;
                case "2":
                    pos.setPayChannel("01000000000000000000");
                    break;
                case "3":
                    pos.setPayChannel("00100000000000000000");
                    break;
                case "4":
                    pos.setPayChannel("00010000000000000000");
                    break;
                case "5":
                    pos.setPayChannel("00001000000000000000");
                    break;
                case "6":
                    pos.setPayChannel("00000100000000000000");
                    break;
                case "7":
                    pos.setPayChannel("00000010000000000000");
                    break;
                case "8":
                    pos.setPayChannel("00000001000000000000");
                    break;
            }
            Date date=new Date();
            pos.setChangeTime(date);
            pos.setCreateTime(date);
            posMapper.insertSelective(pos);
        }
    }
}
